mod wave;

use std::f32::consts::PI;

use ggez::{Context, GameResult, GameError, graphics, glam::Vec2};
use rand::Rng;

pub struct State{
    waves: Vec<wave::Wave>,
}

impl State{
    pub fn new() -> State {
        let mut waves: Vec<wave::Wave> = vec![];
        let mut rng = rand::thread_rng();
        for _i in 0..10 {
            let location = Vec2{x: rng.gen_range(0.0..500.0),y: rng.gen_range(0.0..400.0)};
            let phase = rng.gen_range(0.0..(2.0*PI));
            let freq = rng.gen_range(1.0..100.0);
            let amplitude = rng.gen_range(10.0..50.0);
            let len = rng.gen_range(40.0..400.0);
            let speed = rng.gen_range(0.01..1.0);
            let wave = wave::Wave::new(location, phase, freq, amplitude, len, speed);
            waves.push(wave);
        }
        State { waves }
    }
}

impl ggez::event::EventHandler<GameError> for State {
    fn update(&mut self, _ctx: &mut Context) -> GameResult {
        for wave in &mut self.waves {wave.update()}
        Ok(())
    }
    fn draw(&mut self, ctx: &mut Context) -> GameResult {
        let mut canvas = graphics::Canvas::from_frame(ctx, graphics::Color::WHITE);
        for wave in &mut self.waves {wave.display(ctx, &mut canvas)}
        canvas.finish(ctx)?;
        Ok(())
    }
}